#!/usr/bin/bash
$ grep -q sse4_2 /proc/cpuinfo && echo "SSE 4.2 supported" || echo "SSE 4.2 not supported"

if [ $? -ne 0 ]; then
    echo "当前CPU不支持SSE 4.2的命令，不能进行ClickHouse安装";
    exit;
fi

#安装expect
yum install -y expect

#安装libtool安装
yum install -y libtool

#unixODBC 安装
yum install -y *unixODBC*

#CentOS  取消 SELINUX

#判断文件是否存在
if [[ ! -f "/etc/selinux/config" ]]; then
#文件不存在
 touch /etc/selinux/config
 #自定义reids.conf配置文件
cat >>/etc/selinux/config<<EOF
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted
EOF
else
cd /etc/selinux
find  . -name config|xargs sed -i '/SELINUX=/a SELINUX=disabled'
sed -i 'N;/\n.*SELINUX=disabled/!P;D' config
fi

##设置完毕后要重启linux，可以用命令让其临时生效,0是关闭，1是打开
setenforce 0

#选择clickhouse文件下载存放地址
CLICKHOUSE_DOWNLOAD_PATH=$1
if [ ! $CLICKHOUSE_DOWNLOAD_PATH ];then
	CLICKHOUSE_DOWNLOAD_PATH="/usr/local/clickhouse"
fi

#选择clickhouse的下载地址
CLICKHOUSE_DOWNLOAD_URL=$2
if [ ! $CLICKHOUSE_DOWNLOAD_URL ];then
	CLICKHOUSE_DOWNLOAD_URL="ftp://ftpUser:123456@175.178.190.189/clickhouse/clickhouse_22.3.3.44"
fi

#选择clickhouse的default用户名的密码
CLICKHOUSE_PASSWORD=$3
if [ ! $CLICKHOUSE_PASSWORD ];then
        CLICKHOUSE_PASSWORD=""
fi


#如果文件夹不存在，创建文件夹
if [ ! -d $CLICKHOUSE_DOWNLOAD_PATH ];then
   mkdir $CLICKHOUSE_DOWNLOAD_PATH
fi

#选择clickhouse的端口号
CLICKHOUSE_CLIENT_PORT=$4
if [ ! $CLICKHOUSE_CLIENT_PORT ];then
        CLICKHOUSE_CLIENT_PORT="9020"
fi

cd $CLICKHOUSE_DOWNLOAD_PATH
rm -rf *

#wget下载文件
wget -r -nd  $CLICKHOUSE_DOWNLOAD_URL

if [ $? -ne 0 ]; then
    echo "安装包下载失败，请排查原因，退出脚本!!!";
    exit;
fi

##获取最新的一个文件名
FILENAME=`ls -lt $CLICKHOUSE_DOWNLOAD_PATH | awk 'NR==2 {print $9}'`

#安装前先进行卸载操作，确保稍后解压安装操作不受影响
yum list installed | grep clickhouse
yum remove -y clickhouse-common-static
yum remove -y clickhouse-server-common
rm -rf /var/lib/clickhouse
rm -rf /etc/clickhouse-*
rm -rf /var/log/clickhouse-server

#判断当前文件是什么压缩包
if [[ ${FILENAME##*.} = "rpm" ]];then
	#rpm解压安装，注意: rpm安装不能由用户指定安装目录的，安装目录预先定义好的。
	if [ ! $CLICKHOUSE_PASSWORD ];then
expect << EOF
    set timeout -1spawn
    spawn sh -c "rpm -ivh --replacepkgs  *.rpm"
    expect "*password*"    {send "\r"}
    expect  "*accept connections from the network*"  {send "y\r"}
    expect  "Allow*"  {send "y\r"}  
EOF
#	expect  -c '
#		spawn  rpm -ivh --replacepkgs  *.rpm
#		expect "* password*" { send "\r"}
#		interact
#	'
	else
#这种方法不知道怎么传递参数
#		expect -c '
#		set timeout 60
#		spawn  rpm -ivh --replacepkgs  *.rpm
#       		expect "*password*" { send "$CLICKHOUSE_PASSWORD\r"}
#		interact
#		'
#这种方法可以直接使用定义好的参数       	
expect << EOF
     set timeout -1spawn
     spawn  rpm -ivh --replacepkgs  *.rpm
     expect "*password*"    {send "$CLICKHOUSE_PASSWORD\r"}
     expect  "*accept connections from the network*"  {send "y\r"}
     expect  "Allow*"  {send "y\r"} 
EOF
	fi
	
	#查询所有已经安装的软件包是否已经安装
	rpm -qa|grep clickhouse
	if [ $? -ne 0 ]; then
		echo "clickhouse解压安装失败，请排查原因!!!";
 	 exit;
	 fi
	elif [[ ${FILENAME##*.} = "deb" ]];then
	yum -y install dpkg
	if [ ! $CLICKHOUSE_PASSWORD ];then
    	 	
expect   << EOF
    set timeout -1spawn
    spawn sh -c  "dpkg -i *.deb"
    expect "*password*"  {send "\r";exp_continue }
    expect  "*accept connections from the network*"  {send "y\r"}
    expect  "Allow*"  {send "y\r"}	

EOF
#	yes | dpkg -i *.deb
		 	
	else
expect   << EOF
    set timeout -1spawn
    spawn sh -c  "dpkg -i *.deb"
    expect  "*password*"  {send "$CLICKHOUSE_PASSWORD\r";}
    expect  "*accept connections from the network*"  {send "y\r";}
    expect  "Allow*"  {send "y\r"}	
EOF

#		$CLICKHOUSE_PASSWORD |  dpkg -i *.deb
	fi
	#查询所有已经安装的软件包是否已经安装
	dpkg -l | grep clickhouse
	if [ $? -ne 0 ];then
          echo "clickhouse解压安装失败，请排查原因!!!";
          exit;
	fi
else
	echo "当前只支持deb和rpm安装包，请检查当前下载的安装包,退出脚本";
	exit;
fi

#修改配置文件，修改clickhouse的默认端口号
cd /etc/clickhouse-server
sed -i "/^.*<tcp_port>9000<\/tcp_port>.*$/a <tcp_port>$CLICKHOUSE_CLIENT_PORT<\/tcp_port>" config.xml
sed -i "N;/\n.*<tcp_port>$CLICKHOUSE_CLIENT_PORT<\/tcp_port>/!P;D" config.xml


if [ $? -ne 0 ];then
        echo "-----------------------配置文件修改端口号失败，请排查!!!，退出脚本----------------------";
       exit;
 fi


#修改clickhouse支持IPv4和IPv6
sed -i "/^.*<!--.*<listen_host>::<\/listen_host>.*$/a <listen_host>::<\/listen_host>" config.xml
#sed -i 'N;/\n.*<listen_host>::<\/listen_host>/!P;D' config.xml

 if [ $? -ne 0 ];then
        echo "-----------------------配置文件修改listen_HOST失败，请排查!!!，退出脚本----------------------";
       exit;
 fi


#启动clickhouse
clickhouse start

 if [ $? -ne 0 ];then
        echo "-----------------------clickhouse启动失败，请排查!!!，退出脚本----------------------";
       exit;
 fi

echo "---------------------clichouse安装成功，请登录云服务器,在安全组中放通clickhouse-client端口:TCP-$CLICKHOUSE_CLIENT_PORT 及clickhouse默认HTTP连接端口:TCP-8123---------------------------"
exit
